//
//  CTViewControllerIntercepter.m
//  Interceptor
//
//  Created by casa on 4/17/15.
//  Copyright (c) 2015 alibaba. All rights reserved.
//

#import "AOPViewController.h"
#import <UIKit/UIKit.h>
#import "Aspects.h"

@implementation AOPViewController

+ (void)load
{
    [super load];
    [AOPViewController sharedInstance];
}

/*
    按道理来说，这个sharedInstance单例方法是可以放在头文件的，但是对于目前这个应用来说，暂时还没有放出去的必要
 
    当业务方对这个单例产生配置需求的时候，就可以把这个函数放出去
 */
+ (instancetype)sharedInstance
{
    static dispatch_once_t onceToken;
    static AOPViewController *sharedInstance;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[AOPViewController alloc] init];
    });
    return sharedInstance;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        
        [self trackCommon];
        
        [self trackImagePickerController];
        
        [self trackFirstViewController];
        [self trackSecondViewController];
        [self trackThirdViewController];
    }
    return self;
}

/*
    各位架构师们，这里就是你们可以发挥聪明才智的地方啦。
    你也可以随便找一个什么其他的UIViewController，只要加入工程中，不需要额外添加什么代码，就可以做到自动拦截了。
 
    所以在你原来的架构中，大部分封装UIViewController的基类或者其他的什么基类，都可以使用这种方法让这些基类消失。
 */
#pragma mark - 
#pragma mark  common methods
- (void)loadView:(UIViewController *)viewController
{
    /* 你可以使用这个方法进行打日志，初始化基础业务相关的内容 */
    //NSLog(@"[%@ loadView]", [viewController class]);
}

- (void)viewWillAppear:(BOOL)animated viewController:(UIViewController *)viewController
{
    /* 你可以使用这个方法进行打日志，初始化基础业务相关的内容 */
    NSLog(@"[%@ viewWillAppear:%@]", [viewController class], animated ? @"YES" : @"NO");
}

- (void)viewDidLoad:(UIViewController *)viewController
{
    /* 你可以使用这个方法进行打日志，初始化基础业务相关的内容 */
    // NSLog(@"[%@ viewDidLoad] ", [viewController class]);
}

- (void)trackCommon
{
    /* 在这里做好方法拦截 */
    [UIViewController aspect_hookSelector:@selector(loadView) withOptions:AspectPositionAfter usingBlock:^(id<AspectInfo>aspectInfo){
        [self loadView:[aspectInfo instance]];
    } error:NULL];
    
    [UIViewController aspect_hookSelector:@selector(viewWillAppear:) withOptions:AspectPositionAfter usingBlock:^(id<AspectInfo> aspectInfo, BOOL animated){
        [self viewWillAppear:animated viewController:[aspectInfo instance]];
    } error:NULL];
}

-(void)trackImagePickerController
{
    // We are interested in being notified when the controller is being dismissed.
    [NSClassFromString(@"UIImagePickerController") aspect_hookSelector:@selector(viewWillDisappear:) withOptions:0 usingBlock:^(id<AspectInfo> info, BOOL animated) {
        UIViewController *controller = [info instance];
        if (controller.isBeingDismissed || controller.isMovingFromParentViewController) {
            [[[UIAlertView alloc] initWithTitle:@"Popped"
                                        message:@"Hello from aop"
                                       delegate:nil
                              cancelButtonTitle:nil
                              otherButtonTitles:@"Ok", nil]
             show];
        }
    } error:NULL];
    
    // Hooking dealloc is delicate, only AspectPositionBefore will work here.
    [NSClassFromString(@"UIImagePickerController") aspect_hookSelector:NSSelectorFromString(@"dealloc")
                            withOptions:AspectPositionBefore
                             usingBlock:^(id<AspectInfo> info) {
                                 NSLog(@"UIImagePickerController is about to be deallocated: %@", [info instance]);
                             } error:NULL];
}

#pragma mark -
#pragma mark FirstViewController log
- (void)trackFirstViewController
{
    [NSClassFromString(@"FirstViewController") aspect_hookSelector:@selector(btnClick:) withOptions:AspectPositionAfter usingBlock:^(id<AspectInfo> aspectInfo){
        NSLog(@"[%@ btnClick]", [[aspectInfo instance] class]);
    } error:NULL];
}

#pragma mark -
#pragma mark SecondViewController log
- (void)trackSecondViewController
{
    [NSClassFromString(@"SecondViewController") aspect_hookSelector:@selector(btnClick:) withOptions:AspectPositionAfter usingBlock:^(id<AspectInfo> aspectInfo){
        NSLog(@"[%@ btnClick]", [[aspectInfo instance] class]);
    } error:NULL];
}

#pragma mark -
#pragma mark ThirdViewController log
- (void)trackThirdViewController
{
    [NSClassFromString(@"ThirdViewController") aspect_hookSelector:@selector(btnClick:) withOptions:AspectPositionAfter usingBlock:^(id<AspectInfo> aspectInfo){
        NSLog(@"[%@ btnClick]", [[aspectInfo instance] class]);
    } error:NULL];
}


@end
